<template>
	<view class="assistant">
		<ul v-if="list.length>0">
			<li v-for="(item,index) in list" :key="index">
				<dl>
					<dt>
						<image :src="item.headimgurl" mode=""></image>
					</dt>
					<dd>
						{{item.name}}
					</dd>
				</dl>
				<u-icon name="trash" color="#FC721A" size="20" @click="handleDel(item,index)"></u-icon>
			</li>
			<u-loadmore v-if="hasMore" :status="loadStatus" />
		</ul>
		<view class="empty" v-else>
			<image src="@/static/list.png" mode=""></image>
			<p>No assistants available at the moment</p>
		</view>
		<view class="footer">
			<view class="footer_btn" @click.stop="handleAdd">
				Add an assistant
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAssistant,
		getDelAssistant
	} from "@/lib/business.js";
	import {
		showFailure
	} from '@/lib/helper';
	export default {
		data() {
			return {
				list: [],
				loadStatus: 'loadmore',
				pageNo: 1,
				pageSize: 10,
				hasMore: false,
			}
		},
		onLoad() {
			this.loadData(true)
		},
		onReachBottom() {
			if (this.hasMore) {
				this.pageNo = this.pageNo + 1;
				this.loadData();
			}
		},
		methods: {
			loadData(isReset) {
				let _this = this;
				this.loadStatus = 'loading';
				let obj = {
					pageNo: _this.pageNo,
					pageSize: _this.pageSize
				}
				getAssistant(obj).then((res) => {
					uni.hideLoading();
					this.loadStatus = 'no-more';
					this.hasMore = res.data.hasMore;
					if (isReset) {
						this.list = res.data.records;
					} else {
						this.list = this.list.concat(res.data.records);
					}
				}).catch((res) => {
					this.loadStatus = 'no-more';
					uni.hideLoading();
					showFailure();
				});
			},
			handleDel(item, index) {
				uni.showModal({
					title: 'Delete assistant',
					content: 'Are you sure you want to delete this assistant?',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							getDelAssistant({
								userid: item.userid
							}).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: 'Deleted',
										icon: 'none'
									})
									_this.pageNo = 1;
									_this.list = [];
									_this.loadData(true);

								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			handleAdd() {
				uni.navigateTo({
					url: './add_assistant'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.empty {
		text-align: center;
		margin-top: 10vh;

		image {
			width: 376rpx;
			height: 376rpx;
		}

		p {
			font-size: 28rpx;
			color: #999999;
		}
	}

	.assistant {
		padding: 32rpx;
		padding-bottom: 140rpx;

		ul {
			li {
				margin-top: 40rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				dl {
					display: flex;
					align-items: center;

					dt {
						width: 112rpx;
						height: 112rpx;
						border-radius: 50%;
						background-color: #eee;
						margin-right: 20rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}

					dd {
						font-size: 32rpx;
						font-weight: 500;
						color: #1C120D
					}
				}
			}
		}
	}

	.footer {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		background: #fff;
		padding: 32rpx 0;

		.footer_btn {
			height: 96rpx;
			border-radius: 50rpx;
			text-align: center;
			line-height: 96rpx;
			color: #fff;
			font-size: 32rpx;
			background: #FA751F;

			margin: 0 45rpx;
		}
	}
</style>